const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    winWidth: 0,
    winHeight: 0,
    // tab切换
    currentTab: 0,
    count:'180',
    sell_count:'1',
    sellList:[],
    customerList:[],
    is_show_modal: false,
    m_name:'',
    m_phone:'',
    m_address:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 获取系统信息
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
    
    that.myRequest()
  },
  myRequest:function(){
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function (res) {
        let openid = res.data
        wx.showLoading({
          title: '加载中',
        })
        wx.request({
          url: app.globalData.URL + '/app/mycard/cardindex?openid=' + openid,
          success: function (res) {
            wx.hideLoading()
            console.log(res)
            that.setData({
              count: res.data.cardNum,
            })
          }
        })
      },
    })
  },
  // 滑动切换tab
  bindChange: function (e) {
    var that = this;
    that.setData({ currentTab: e.detail.current });
  },

  // 点击tab切换
  swichNav: function (e) {
    var that = this;
    if (this.data.currentTab === e.target.dataset.current) {
      return false;
    } else {
      that.setData({
        currentTab: e.target.dataset.current
      })
    }
    console.log("当前tab",that.data.currentTab)

    if(that.data.currentTab == '1'){
      wx.getStorage({
        key: 'openid',
        success: function (res) {
          let openid = res.data
          wx.request({
            url: app.globalData.URL + '/app/mycard/ysCard?openid=' + openid,
            success: function (res) {
              console.log('已售代理', res)
              that.setData({
                sellList: res.data.list,
              })
            }
          })
        },
      })
      
    }else if(that.data.currentTab == '2'){
      wx.getStorage({
        key: 'openid',
        success: function (res) {
          let openid = res.data
          wx.request({
            url: app.globalData.URL + '/app/mycard/ysGuke?openid=' + openid,
            success: function (res) {
              console.log('已售顾客', res)
              that.setData({
                customerList: res.data.list,
              })
            }
          })
        },
      })
      
    } else if (that.data.currentTab == '0'){
      that.myRequest()
    }
  },

  showModal: function () {
    this.setData({
      is_show_modal: true
    })
  },
  draw:function(){
    var that = this
    wx.getStorage({
      key: 'openid',
      success: function(res) {
        let openid = res.data

        wx.request({
          url: app.globalData.URL + '/app/mycard/tiquCard?openid='+openid + '&name=' + that.data.m_name + '&phone=' + that.data.m_phone + '&address=' + that.data.m_address,
          success: function (res) {
            console.log("tiquCard",res.data.flag)
            
            if (res.data.flag == true) {
              wx.showToast({
                title: '提取成功',
                duration: 5000
              })
              that.setData({
                is_show_modal:false
              })
              that.myRequest()
            } else {
              wx.showToast({
                title: '请求数据错误，请稍后再试',
                duration: '5000',
                image: "../../images/error.png"
              })
            }

          }
        })
      },
    })  
  },

  nameInput:function(e){
    this.setData({
      m_name: e.detail.value
    })
  },
  phoneInput: function (e) {
    this.setData({
      m_phone: e.detail.value
    })
  },
  addressInput: function (e) {
    console.log(e)
    this.setData({
      m_address: e.detail.value
    })
  },
  hideModal:function(){
    this.setData({
      is_show_modal:false
    })
  }
})